import { api } from '../boot/axios'
import { DashBoardData, HistorySearch, InOutSummary, OutgoingRecord, RegistrationSummary, RepairingOverview, RepairingOverviewTable, RepairOrderHistory, RepairOrderNFFRecord, SpareAvailabilityI, Total } from './type'

function url(s: string) {
  if (s==''){
    return ['/api/report', ''].join('/')
  }
  return ['/api/report', s.replace('/', ''), ''].join('/')
}


export async function repairOrderHistory(filter?: HistorySearch) {
  console.log(filter);
  
  const res = await api({
    url: url('repair_order_history'),
    method: 'post',
    data: {
      stock_code: filter?.stock_code,
      equipment_class: filter?.equipment_class,
      level: filter?.level,
      location: filter?.location,
      dateTo: filter?.dateTo,
      dateFrom: filter?.dateFrom,
    }
  })
  return res.data.data as RepairOrderHistory[]
}

export async function getNFFRecord() {
  const res = await api({
    url: url('repair_order_nff_records'),
    method: 'get',
  })
  return res.data.data as RepairOrderNFFRecord[]
}

export async function repairingOverview(system:string, year:string) {
  const res = await api({
    url: url('repair_order_detail_overview'),
    method: 'post',
    data: {
      system,
      year
    }
  })
  return res.data.data as RepairingOverview[] & Total
}

export async function inoutSummary() {
  const res = await api({
    url: url('inout_summary'),
    method: 'get',
  })
  return res.data.data as InOutSummary
}

export async function initRegistration() {
  const res = await api({
    url: url('registration_summary'),
    method: 'get',
  })
  return res.data.data as RegistrationSummary
}

export async function outgoingRecord(year: string, month: string) {
  const res = await api({
    url: url('outgoing_record'),
    method: 'post',
    data: {
      year,
      month
    }
  })
  return res.data.data as OutgoingRecord[]
}

export async function getRepairingOverView(repair_detail:string, system: string, year: string) {
  const res = await api({
    url: url('repair_order_detail_table'),
    method: 'post',
    data: {
      repair_detail,
      system,
      year
    }
  })
  return res.data.data as RepairingOverviewTable[]
}

export async function getSpareAvailability( year: string, month: string) {
  const res = await api({
    url: url('get_spare_availability'),
    method: 'post',
    data: {
      year,
      month,
      
    }
  })
  return res.data.data as SpareAvailabilityI[]
}

export async function getOverhaulIndex( year: string, month: string) {
  const res = await api({
    url: url('get_overhaul_index'),
    method: 'post',
    data: {
      year,
      month
    }
  })
  return res.data.data as RepairingOverviewTable[]
}

export async function getWorkgroupIndex( year: string, month: string) {
  const res = await api({
    url: url('chart_repair_time_of_various_workgroup'),
    method: 'post',
    data: {
      year,
      month
    }
  })
  return res.data.data as RepairingOverviewTable[]
}
export async function getLongOutstanding( year: string, month: string) {
  const res = await api({
    url: url('get_long_outstanding'),
    method: 'post',
    data: {
      year,
      month
    }
  })
  return res.data.data as DashBoardData[]
}
export async function getchart_repair_time_of_various_workgroup( year: string, month: string) {
  const res = await api({
    url: url('chart_repair_time_of_various_workgroup'),
    method: 'post',
    data: {
      year,
      month
    }
  })
  return res.data.data as DashBoardData[]
}
export async function getIncidentRepairSummary( year: string, month: string) {
  const res = await api({
    url: url('repair_performance_summary'),
    method: 'post',
    data: {
      year,
      month
    }
  })
  return res.data.data as DashBoardData[]
}

export async function getCriticalHardTimeReplacement() {
  const res = await api({
    url: url('get_critical_hard_time_replacement'),
    method: 'get',
  })
  return res.data.data as DashBoardData[]
}